package com.aaamvc.util;

import com.aaamvc.action.UserAction;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;

public class StringUtil {

	
	public static void main(String[] args) {
//		Class clazz = UserAction.class;
		try {
			ClassPool pool = ClassPool.getDefault();
//			System.out.println("===========" + clazz.getName());
			CtClass cc = pool.get("com.aaamvc.action.UserAction");
			CtMethod cm = cc.getDeclaredMethod("save");

			// 使用javaassist的反射方法获取方法的参数名
			MethodInfo methodInfo = cm.getMethodInfo();
			CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
			LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
			if (attr == null) {
				// exception
			}
			String[] paramNames = new String[cm.getParameterTypes().length];
			int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
			for (int i = 0; i < paramNames.length; i++)
				paramNames[i] = attr.variableName(i + pos);
			// paramNames即参数名
			for (int i = 0; i < paramNames.length; i++) {
				System.out.println(paramNames[i]);
			}

		} catch (NotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static String getActionName(String url,String ctx){
		
		int  p2 = url.indexOf(".");
		int p1 = ctx.length();
		String actionName = url.substring(p1, p2);
		return actionName;
	}
}
